/*
 * Project it.battlehorse.rcp.tools
 * Created on Nov 18, 2005
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * A copy of the LGPL is available also at
 * http://www.gnu.org/copyleft/lesser.html
 * 
 */ 
package it.battlehorse.rcp.tools;

import it.battlehorse.rcp.tools.actions.RunActorAction;

import org.eclipse.jface.action.ActionContributionItem;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.IContributionItem;
import org.eclipse.jface.action.ICoolBarManager;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.jface.action.ToolBarManager;
import org.eclipse.swt.SWT;
import org.eclipse.ui.IWorkbenchActionConstants;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.actions.ActionFactory;
import org.eclipse.ui.actions.ContributionItemFactory;
import org.eclipse.ui.application.ActionBarAdvisor;
import org.eclipse.ui.application.IActionBarConfigurer;

/**
 * The action bar advisor
 * 
 * @author battlehorse
 * @since Nov 18, 2005
 */
public class ApplicationActionBarAdvisor extends ActionBarAdvisor {
	
	IActionBarConfigurer configurer;
	
	private IAction runActorAction;
	
	private IAction preferencesAction; 

    public ApplicationActionBarAdvisor(IActionBarConfigurer configurer) {
        super(configurer);
        this.configurer = configurer;
    }

    @Override
	protected void makeActions(IWorkbenchWindow window) {
    	runActorAction = new RunActorAction();
    	
    	preferencesAction = ActionFactory.PREFERENCES.create(window);
    	preferencesAction.setImageDescriptor(Activator.getImageDescriptor("resources/prefs/prefs.png"));
    }

    @Override
	protected void fillMenuBar(IMenuManager menuBar) {
    	menuBar.add(createFileMenu());
    	menuBar.add(createActionsMenu());
    	menuBar.add(createWindowMenu());
    	menuBar.add(createAboutMenu());
    }
    
    private MenuManager createFileMenu() {
    	MenuManager menu = new MenuManager("&File",IWorkbenchActionConstants.M_FILE);
    	menu.add(runActorAction);
    	menu.add(new Separator());
    	menu.add(ActionFactory.QUIT.create(configurer.getWindowConfigurer().getWindow()));

    	return menu;
    }
    
    private MenuManager createActionsMenu() {
    	MenuManager menu = new MenuManager("&Actions","it.battlehorse.rcp.tools.actions");   
    	menu.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));    	
    	
    	return menu;
    }
    
    private MenuManager createWindowMenu() {
    	MenuManager menu = new MenuManager("&Window",IWorkbenchActionConstants.M_WINDOW);
    	
    	IWorkbenchWindow window = configurer.getWindowConfigurer().getWindow();    	
    	
    	MenuManager perspectiveMenu = new MenuManager("Open Perspective", "openPerspective"); 
    	IContributionItem perspectiveList = ContributionItemFactory.PERSPECTIVES_SHORTLIST.create(window);
		perspectiveMenu.add(perspectiveList);
		menu.add(perspectiveMenu);		
    	
    	// menu.add(ActionFactory.OPEN_PERSPECTIVE_DIALOG.create(configurer.getWindowConfigurer().getWindow()));
    	
		MenuManager viewMenu = new MenuManager("Show View");
		IContributionItem viewList = ContributionItemFactory.VIEWS_SHORTLIST.create(window);
		viewMenu.add(viewList);
		menu.add(viewMenu);
		
    	//menu.add(ActionFactory.SHOW_VIEW_MENU.create(configurer.getWindowConfigurer().getWindow()));
    	
    	menu.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
		
    	menu.add(ActionFactory.RESET_PERSPECTIVE.create(window));

    	menu.add(new Separator());
    	menu.add(preferencesAction);
    	
    	return menu;
    }
    
    private MenuManager createAboutMenu() {
    	MenuManager menu = new MenuManager("&About","About");
    	IWorkbenchWindow window = configurer.getWindowConfigurer().getWindow(); 
    	menu.add(ActionFactory.ABOUT.create(window));
    	
    	return menu;
    }
    
    @Override protected void fillCoolBar(ICoolBarManager coolBar) {    
    	ToolBarManager toolBar = new ToolBarManager(coolBar.getStyle() | SWT.RIGHT);
    	
    	ActionContributionItem runCI = 
    			new ActionContributionItem(runActorAction); 
    	runCI.setMode(ActionContributionItem.MODE_FORCE_TEXT);     	 
    	toolBar.add(runCI);
    	
    	ActionContributionItem prefsCI = 
			new ActionContributionItem(preferencesAction); 
    	prefsCI.setMode(ActionContributionItem.MODE_FORCE_TEXT);     	 
    	
    	toolBar.add(prefsCI);
    	coolBar.add(toolBar);
    }
    
}
